﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Brain
{
    public static class IntUtil
    {
        /// <summary>
        /// Returns all the values on the list / list.count
        /// </summary>
        /// <param name="ints"></param>
        /// <returns></returns>
        public static int Average(List<int> ints)
        {
            int final = 0;
            for (int i = 0; i < ints.Count; i++)
                final += ints[i];
            final /= ints.Count;
            return final;
        }

        /// <summary>
        /// Clamps the value(Never goes minor than minValue, never goes greater than maxValue. 
        /// This is equivalent to the MathHelper.Clamp(...)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public static int Clamp(int value, int minValue, int maxValue)
        {
            if (value < minValue)
                value = minValue;
            if (value > maxValue)
                value = maxValue;

            return value;
        }

        /// <summary>
        /// Returns the value that is the closes in the array to the value
        /// </summary>
        /// <param name="ints"></param>
        /// <returns></returns>
        public static int Closest(int[] ints, int value)
        {
            int lastNumber = 0;
            int lastDifference = int.MaxValue;

            for (int i = 0; i < ints.Length; i++)
            {
                int v = ints[i];
                int dif = Math.Abs(value - v);
                if (dif < lastDifference)
                {
                    if (dif == 0)
                    {
                        return v;
                    }
                    lastDifference = dif;
                    lastNumber = v;
                }
            }

            return lastNumber;
        }

        /// <summary>
        /// Converts from a Float, to an Int with the desired Mid Value
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ConvertFromFloat(float value, float midValue)
        {
            return (int)(value + midValue);
        }

        /// <summary>
        /// Creates from an Int string
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int CreateFromString(string str)
        {
            return int.Parse(str, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Converts a Bool to an int.
        /// If True, 1, false, 0
        /// </summary>
        /// <param name="boolean"></param>
        /// <returns></returns>
        public static int ConvertFromBool(bool boolean)
        {
            return boolean ? 1 : 0;
        }
    }
}
